<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * function:微信前端主页控制器
 * Date: 2017/8/15
 * Time: 13:24
 */
namespace app\index\controller;
use       think\Controller;
use       think\Request;
use       think\Db;
use       think\Cache;
class  Upload extends Controller
{


	//上传音频
public function uploadvoice()
{
    $MediaId=Request::instance()->post('serverId');
    if(jugement_token_value())
    {
       $access_token=get_access_token_ache();
    }
    else{
    $access_token=$this->getAccess_token();
    }    
    try{  
         
            $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$MediaId";
            $ch = curl_init($url);  
            curl_setopt($ch, CURLOPT_HEADER, 0);  
            curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头  
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
            $package = curl_exec($ch);  
            $httpinfo = curl_getinfo($ch);  
            curl_close($ch);  
            //var_dump($httpinfo);
            //var_dump($package);
             $media = array_merge(array('mediaBody' => $package), $httpinfo);
             //求出文件格式
            preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
            $fileExt = $extmatches[1];

            //$this->saveWeixinFile($filename, $package);  
             $output_directory = 'uploads/voice/'.date("Y-m-d")."/"; //设置图片存放路径
            // /* 检查并创建图片存放目录 */
             if (!file_exists($output_directory)) {
             mkdir($output_directory, 0777);
             }
             $local_file = fopen($output_directory.$MediaId.".{$fileExt}",'w');
             $state=fwrite($local_file, $package);  
             fclose($local_file);
             //将amr格式装换成mp3格式
             $commod="ffmpeg -i "."D:/workspace/zizaimeigui/Public/uploads/voice/".date("Y-m-d")."/".$MediaId.".amr "."-ab 32000 "."D:/workspace/zizaimeigui/public/uploads/voice/".date("Y-m-d")."/".$MediaId.".mp3";
             exec($commod); 
        }  
        catch (Exception $e){   
            return "";
        }

}
//获取access_toen
 public function getAccess_token()
    {
        $appid="wxcd4dce13c9fb1a0a";   //微信公众号唯一标识
        $secret="a9f3c9753b70b42573fdd6440090c1c6";  //秘钥
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $json=$this->http_url($url);
        $result=json_decode($json);
        //写入自己定义的缓存文件
        $data=array();
        $data['access_token']=$result->access_token;
        $data['time_out']=time();
        $string=json_encode($data);
        file_put_contents('token.txt',$string);
        return $result->access_token;
    }
//curl
public function  http_curl($url,$data=null){
		//1.初始化，创建一个新cURL资源
		$ch = curl_init();
		//2.设置URL和相应的选项
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_HEADER, 0);
		if(!empty($data)){
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		}
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不检测服务器的证书是否由正规浏览器认证过的授权CA颁发
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不检测服务器的域名与证书上的是否一致
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//禁止curl资源直接输出
		//3.抓取URL并把它传递给浏览器
		$opt=curl_exec($ch);
		//4.关闭cURL资源，并且释放系统资源
		curl_close($ch);
		return $opt;
}
}


?>